package by.vkhalko.asamo.services.impl;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.vkhalko.asamo.dataaccess.JobTitleDao;
import by.vkhalko.asamo.datamodel.JobTitle;
import by.vkhalko.asamo.services.JobTitleService;

@Service
public class JobTitleServiceImpl implements JobTitleService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(JobTitleServiceImpl.class);

	@Inject
	private JobTitleDao dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of JobTitleService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public JobTitle get(Long id) {
		JobTitle entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(JobTitle jobTitle) {
		if (jobTitle.getId() == null) {
			LOGGER.debug("Save new: {}", jobTitle);
			dao.insert(jobTitle);
		} else {
			LOGGER.debug("Update: {}", jobTitle);
			dao.update(jobTitle);
		}
	}

	@Override
	public void delete(JobTitle jobTitle) {
		LOGGER.debug("Remove: {}", jobTitle);
		dao.delete(jobTitle.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all job titles");
		dao.deleteAll();
	}

}
